<?php

namespace Parse;

class CombinatorParser extends Parser
{
    public function __construct($parsers, $callback = null)
    {
        if (!is_array($parsers)) {
            throw new ParseException("Parsers need to be in array");
        }
        $this->parsers = $parsers;
        parent::__construct($callback);
    }
    public function defaultCallback()
    {
        return func_get_args();
    }

    public function matchProcedure($string)
    {
        $matches = array();
        foreach ($this->parsers as $parser) {
            $result = $parser->match($string);
            $matches[] = $result["match"];
            $string = $result["rest"];
        }
        return array("matches" => $matches,
                     "rest" => $string);
    }
}
